<?php
namespace App\Models\System;

use App\Exceptions\ApiException;
use App\Providers\LengthAwarePaginatorProvider;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Enums\System\SysJobLog\{Status};

/**
* 定时任务调度日志表 模型
* @create 2023-08-22 10:02:28
*/
class SysJobLogModel extends \App\Models\BaseModel {
    use HasFactory;

    const Table         = "sys_job_log";
    const PrimaryKey    = "job_log_id";
    public $timestamps  = true;

    static $Wheres = ['job_log_id','status'];     // 直接定义column, 默认where(column, '=', value)
    public function __construct($attributes = []) {
        parent::__construct($attributes);

        // static::$Wheres['job_log_id']        = fn($q, $v) => $q->where(atn('job_log_id'), '=', $v);
    }

    /** xxxx */
    // public function user() {
        // return $this->hasOne(User::class, 'id', 'uid');
    // }

    /** ========== 常用查询 ========== */
    static $Vo  = ['*', ];
    static $Voi = ['*', ];

    
    /** 根据JobLogId获取 */
    public function selectByJobLogId($job_log_id): SysJobLogModel {
        $where = [['job_log_id', '=', "{$job_log_id}"]];
        return throw_str(
            static::row($where) ?? "数据($job_log_id)为空~"
        );
    }


    /** 获取列表 @return SysJobLog[]+paginator() */
    public function selectList(int|array $pag=1, array $param=[]): LengthAwarePaginatorProvider {
        return static::rows_pag($pag, [], fn($q)=>$q
                    // ->when(isset($param['job_log_id']), fn($q)=>$q->where('job_log_id', '=', $param['job_log_id']))
                    ->wheres($param, 'job_log_id','status')
                    ->orderByDesc("job_log_id")
        );
    }


    /** 插入数据 */
    public function insertData(array $data): SysJobLogModel {
        return throw_str(
            static::row_add([
                // 'job_log_id' => $data['job_log_id'],
                'job_name' => $data['job_name'],
                'job_group' => $data['job_group'],
                'invoke_target' => $data['invoke_target'],
                'job_message' => $data['job_message'] ?? '',
                'status' => $data['status'] ?? '0',
                'exception_info' => $data['exception_info'] ?? '',
                // 'create_time' => $data['create_time'] ?? '',
            ])
        );
    }


    /** 更新数据 */
    public function updateData(int|array $idOrWhere, array $data): SysJobLogModel {
        $values = static::when(
                    $data, 
                    // 'job_log_id',
                    'job_name',
                    'job_group',
                    'invoke_target',
                    'job_message',
                    'status',
                    'exception_info',
                    // 'create_time',
                );
        return throw_str(
            static::row_update($idOrWhere, $values)
        );
    }


    /** 删除数据 */
    public function deleteData(int|array $idOrWhere): SysJobLogModel {
        return throw_str(
            static::row_delete($idOrWhere)
        );
    }



}